<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/8
 * Time: 10:46
 */
class IMSupportController
{
    private $pushService;
    private $userService;
    private $messageService;
    private $chatGroupService;

    function __construct()
    {
        $this->pushService          =   new PushService();
        $this->userService          =   new UserService();
        $this->messageService       =   new MessageService();
        $this->chatGroupService     =   new ChatGroupService();
    }

    /**
     * 重载无访问的action
     * @param $functionName
     * @param $args
     * @return mixed
     */
    function __call($functionName,$args)
    {
        AC::$errorNo = 'SA004';
        return fasle;
    }

    /**
     * 路由
     * @param $param
     * @return bool|string
     */
    public function callApi($param)
    {
        return $this->$param['op']($param);
    }

    /**
     * 私信推送
     * @param $param
     * @return bool
     */
    private function pushToUser($param)
    {
        $fromUid  = $host_uid = intval($param['fromUid']);
        $toUid    = intval($param['toUid']);
        $content  = UK::getstr($param['content'], 150);
        if (empty($fromUid) || empty($toUid) || ($fromUid == $toUid) || strlen($content) < 1)
        {
            return false;
        }

        $fromUser = $this->userService->getUserInfo($fromUid);
        $toUser   = $this->userService->getUserInfo($toUid);
        if (empty($fromUser) || empty($toUser))
        {
            return false;
        }

        $action  = 'pushMessage';
        $data = array(
            'uid'           =>    $fromUid,
            'mUid'          =>    $toUid,
            'nickname'      =>    $fromUser['nickname'],
            'mNickname'     =>    $toUser['nickname'],
            'content'       =>    $content,
            'eventType'     =>    '',
            'eventTypeGid'  =>    ''
        );

        return $this->pushService->pushToRedisList($data, $action, $toUid, $host_uid);
    }

    /**
     * 群组推送
     * @param $param
     * @return bool|void
     */
    private function pushToGroup($param)
    {
        $uid = intval($param['uid']);
        $gid = intval($param['gid']);
        $content = UK::getstr($param['content'], 500);
        if (empty($uid) || empty($gid) || strlen($content) < 1)
        {
            return false;
        }

        $user = $this->userService->getUserInfo($uid);
        $group = $this->chatGroupService->getChatGroupInfo($gid);
        if (empty($user) || empty($group))
        {
            return false;
        }

        $data = array(
            'uid'      =>    $uid,
            'nickname' =>    $user['nickname'],
            'gid'      =>    $gid,
            'name'     =>    $group['name'],
            'remark'   =>    '',
            'content'  =>    $content
        );

        return $this->pushService->pushToTag($data);
    }

    /**
     * 获取群组缓存信息
     * @param $param
     * @return Array|bool
     */
    private function getChatGroupInfo($param)
    {
        $uid = intval($param['uid']);
        $gid = intval($param['gid']);
        if (empty($gid) || empty($uid))
        {
            return false;
        }

        $group = $this->chatGroupService->getChatGroupInfo($gid, $uid);
        if (empty($group)) return (Object) array();
        
        $group['userlist'] = $this->chatGroupService->getChatGroupUsers($gid, $uid);
        return $group;
    }

    /**
     * 用户群组关系
     * @param $param
     * @return array|bool|object
     */
    private function getUserJoinedChatGroups($param)
    {
        $uid = intval($param['uid']);
        if (empty($uid))
        {
            return false;
        }

        $user = $this->userService->getUserInfo($uid);
        if (empty($user)) return (Object) array();

        return $this->chatGroupService->getUserJoinedChatGroupIds($uid);
    }

    /**
     * 群组用户关系
     * @param $param
     * @return array|bool|object
     */
    private function getChatGroupMembers($param)
    {
        $gid = intval($param['gid']);
        if (empty($gid))
        {
            return false;
        }

        $chatGroup = $this->chatGroupService->getChatGroupInfo($gid);
        if (empty($chatGroup)) return (Object) array();

        return $this->chatGroupService->getChatGroupMemberIds($gid);
    }

    /**
     * 获取用户的群组会话列表
     * @param $param
     * @return bool
     */
    private function getUserJoinedGroups($param)
    {
        $uid = intval($param['uid']);
        if (empty($uid))
        {
            return false;
        }

        $user    = $this->userService->getUserInfo($uid);
        if (empty($user))
        {
            return false;
        }

        return $this->chatGroupService->getUserJoinedGroups($uid);
    }

    /**
     * 清除未读数
     * @param $param
     * @return bool
     */
    private function resetUnReadNum($param)
    {
        $uid = intval($param['uid']);
        $mUid = intval($param['mUid']);
        if (empty($uid) || empty($mUid) || $uid == $mUid)
        {
            return false;
        }

        $user    = $this->userService->getUserInfo($uid);
        $mUser   = $this->userService->getUserInfo($mUid);
        if (empty($user) || empty($mUser))
        {
            return false;
        }

        $listInfo   =   $this->messageService->getMessageListInfo($uid, $mUid);
        if ($listInfo)
        {
            $this->userService->updateUserStatistics($uid, array('messageTimes' => - abs($listInfo['unReadNum'])));
            $this->messageService->resetUnReadNum($uid, $mUid);
        }

        return true;
    }

    /**
     * 同步workman
     * @param $param
     * @return bool|mixed
     */
    private function syncToWorkMan($param)
    {
        $uid = intval($param['uid']);
        $gid = intval($param['gid']);
        $action = strval($param['type']);
        if (empty($uid) || empty($gid) || empty($action))
        {
            return false;
        }

        return $this->chatGroupService->syncToWorkMan($uid, $action, $gid, $uid);
    }

    private function checkSendMsg($param){
        $uid = intval($param['uid']);
        $mUid = intval($param['mUid']);
        $gid = intval($param['gid']);
        if ($gid>0){
            return true;
        }
        if (empty($uid) || empty($mUid) || $uid == $mUid)
        {
            return false;
        }

        $user    = $this->userService->getUserInfo($uid);
        if ($user['level']>5 || $user['is_vip']){
            return true;
        }
        return false;
    }
}